# -*- coding: utf-8 -*-
"""
配置文件，存储所有常量配置
"""

import os
import json


def _get_bool(name, default=False):
    value = os.getenv(name)
    if value is None:
        return default
    value_lower = value.strip().lower()
    return value_lower in ("1", "true", "yes", "on")


def _get_int(name, default):
    raw = os.getenv(name)
    if raw is None:
        return default
    try:
        return int(raw)
    except ValueError:
        return default


# 全局配置（支持环境变量，附带安全默认值）
USE_SILICONFLOW = _get_bool("USE_SILICONFLOW", False)  # 是否使用硅基流动配置

# 文本长度配置（支持环境变量，附带默认值）
MAX_TEXT_LENGTH = _get_int("MAX_TEXT_LENGTH", 15000)  # 最大文本长度阈值
SUMMARY_TARGET_LENGTH = _get_int("SUMMARY_TARGET_LENGTH", 3000)  # 摘要目标长度

# LLM服务配置（支持环境变量，API Key 不再硬编码）
LLM_CONFIG = {
    "BASE_URL_1238": os.getenv("LLM_BASE_URL_1238", "http://156.22.30.238:1238"),
    "BASE_URL_1244": os.getenv("LLM_BASE_URL_1244", "http://156.22.30.238:1244"),
    "BASE_URL_dev": os.getenv("LLM_BASE_URL_DEV", "https://api.siliconflow.cn"),
    "MODEL_14B": os.getenv("LLM_MODEL_14B", "qwen2.5-14b"),
    "MODEL_72B": os.getenv("LLM_MODEL_72B", "qwen2.5-72b"),
    "MODEL_dev": os.getenv("LLM_MODEL_DEV", "Qwen/QwQ-32B"),
    "MODEL_embedding": os.getenv("LLM_MODEL_EMBEDDING", "Qwen/Qwen3-Embedding-0.6B"),
    "API_KEY": os.getenv("LLM_API_KEY", ""),
}

# 字典类型配置（可通过 JSON 环境变量整体覆盖）
_default_dict_types = {
    "问题性质": "file/训练模型字典.xlsx",
    "统一关键字": "file/训练模型字典.xlsx",
    "自定义关键字": "file/训练模型字典.xlsx",
    "信访性质": "file/训练模型字典.xlsx",
    "干部管理权限": "file/反映人被反映人字典.xlsx",
    "职级": "file/反映人被反映人字典.xlsx",
    "政治面貌": "file/反映人被反映人字典.xlsx",
}

_dict_types_json = os.getenv("DICT_TYPES_JSON")
if _dict_types_json:
    try:
        DICT_TYPES = json.loads(_dict_types_json)
    except Exception:
        DICT_TYPES = _default_dict_types
else:
    DICT_TYPES = _default_dict_types

# 字典工作表配置（可通过 JSON 环境变量整体覆盖）
_default_dict_sheets = {
    "问题性质": "最后一级问题性质",
    "统一关键字": "统一关键字",
    "自定义关键字": "自定义关键字",
    "信访性质": "信访性质",
    "干部管理权限": "干部管理权限",
    "职级": "职级",
    "政治面貌": "政治面貌",
}

_dict_sheets_json = os.getenv("DICT_SHEETS_JSON")
if _dict_sheets_json:
    try:
        DICT_SHEETS = json.loads(_dict_sheets_json)
    except Exception:
        DICT_SHEETS = _default_dict_sheets
else:
    DICT_SHEETS = _default_dict_sheets
